package com.itc.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itc.common.core.domain.R;
import com.itc.common.core.validate.Add;
import com.itc.common.core.validate.Update;
import com.itc.common.security.annotation.InnerAuth;
import com.itc.common.security.utils.SecurityUtils;
import com.itc.system.api.domain.SysUser;
import com.itc.system.api.model.LoginUser;
import com.itc.system.domain.dto.SwitchSeatDto;
import com.itc.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/user")
@Api(value = "sysUser", tags = "系统用户管理")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 切换席位
     * @param dto
     * @return
     */
    @ApiOperation(value = "切换席位")
    @PostMapping("/switch/seat")
    public R switchSeat (@RequestBody @Validated SwitchSeatDto dto){
        if (dto.getUserId() == null){
            dto.setUserId(SecurityUtils.getUserId());
        }
        return sysUserService.switchSeat(dto);
    }

    /**
     * 获取当前用户信息
     */
    @InnerAuth
    @GetMapping("/info/{username}")
    public R<LoginUser> getUserInfoByName(@PathVariable("username") String username)
    {
        return sysUserService.getUserInfoByName(username);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/login/info")
    public R<JSONObject> getLoginUserInfo()
    {
        return sysUserService.getLoginUserInfo();
    }

    /**
     * 添加用户
     * @param sysUser @return
     */
    @ApiOperation(value = "添加用户")
    @PostMapping("/add")
    public R addUser(@Validated(Add.class) @RequestBody SysUser sysUser){
        return sysUserService.addUser(sysUser);
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @ApiOperation(value = "注销用户")
    @DeleteMapping("/delete/{userId}")
    public R deleteUser(@PathVariable(value = "userId") Long userId){
        return sysUserService.deleteUser(userId);
    }

    /**
     * 修改用户
     * @param sysUser
     * @return
     */
    @ApiOperation(value = "修改用户")
    @PostMapping("/update")
    public R updateUser(@Validated(Update.class) @RequestBody SysUser sysUser){
        return sysUserService.updateUser(sysUser);
    }

    /**
     * 重置密码
     * @param userId
     * @return
     */
    @ApiOperation(value = "重置密码")
    @GetMapping("/reset/pwd/{userId}")
    public R resetPwd(@PathVariable(value = "userId") Long userId){
        return sysUserService.resetPwd(userId);
    }

    /**
     * 查询用户详细信息
     * @param userId
     * @return
     */
    @ApiOperation(value = "查询用户详细信息")
    @GetMapping("/detail/{userId}")
    public R<SysUser> getSysUser( @PathVariable Long userId){
        return sysUserService.queryUser(userId);
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询用户")
    public R<Page<SysUser>> getPage(@RequestBody SysUser sysUser)
    {
        Page<SysUser> page = new Page<>();
        page.setOptimizeCountSql(true);
        page.setSize(sysUser.getSize());
        page.setCurrent(sysUser.getCurrent());
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>(sysUser);
        queryWrapper.eq(SysUser::getDelFlag,0);
        Page<SysUser> resultPage = sysUserService.page(page,queryWrapper);
        return R.ok(resultPage);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询全部用户信息")
    public R<List<SysUser>> getList(@RequestBody SysUser sysUser)
    {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>(sysUser);
        queryWrapper.eq(SysUser::getDelFlag,0);
        List<SysUser> resultList = sysUserService.list(queryWrapper);
        return R.ok(resultList);
    }

    @ApiOperation(value = "根据部门id查询用户")
    @GetMapping("/list/dept/{deptId}")
    public R<List<SysUser>> listByDeptId(@PathVariable(value = "deptId") Long deptId){
        return sysUserService.listByDeptId(deptId);
    }
}
